package com.zhongxi.guoyuan.fragment.beanexchange;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.OffLineShop.OffLineBaseFragment;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.personExchangeOrder.PersonExchangeNoPayAdapter;
import com.zhongxi.guoyuan.adapter.personExchangeOrder.PersonExchangeNoSendAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.clearweed.PersonExchangeResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.UiUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExchangeNoSendFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2<ListView>{

    private PersonExchangeNoSendAdapter mNoPayAdapter;


    private List<PersonExchangeResult.OrderInfo> mInfoList = new ArrayList<>();
    private String orderCus;
    private String shopInfo;
    private PersonExchangeResult personExchangeResult;
    private String type;

    @Bind(R.id.pullToRefreshListView)
    PullToRefreshListView mPullToRefreshListView;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    public ExchangeNoSendFragment() {
        // Required empty public constructor
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_exchange_no_pay, container, false);
        ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    protected void initView() {
        super.initView();
        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mPullToRefreshListView.setOnRefreshListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        mNoPayAdapter = new PersonExchangeNoSendAdapter(mHandler, getContext());
        mPullToRefreshListView.setAdapter(mNoPayAdapter);
        getData();
    }

    public void getData() {
        type = "order";
        String str = "&page=" + pageNum;
        getServer(Url.EXCHANGE_NOSEND, null, str, false, null, "get");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden)
            getData();
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson mGson = new Gson();
        personExchangeResult = mGson.fromJson(response, PersonExchangeResult.class);
        MyApplication.getInstance().setSpValue(personExchangeResult.getSafeobj());
        if (personExchangeResult.getCode() == 1) {
            if (mPullToRefreshListView.isRefreshing())
                mPullToRefreshListView.onRefreshComplete();

            if (pageNum == 1) {
                mInfoList.clear();
            }

            if (personExchangeResult.getOrderInfo().size()>0){
                mInfoList.addAll(personExchangeResult.getOrderInfo());
            }else {
                if (pageNum!=1)
                    pageNum--;
            }

            try {
                JSONObject mJSONObject=new JSONObject(response);
                orderCus=mJSONObject.optString("orderCus");
                shopInfo=mJSONObject.optString("shopInfo");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            mNoPayAdapter.setData(mInfoList, orderCus, shopInfo);
            mNoPayAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson=new Gson();
        BaseResponse mBaseResponse=gson.fromJson(response,BaseResponse.class);
        if(null!=mBaseResponse.getSafeobj()&&!"".equals(mBaseResponse.getSafeobj())){
            MyApplication.getInstance().setSpValue(mBaseResponse.getSafeobj());
        }

    }
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;
        getData();

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
